class Solution
{
public:
    unordered_map<int, int> search;
    X
        TreeNode *
        _bulid(vector<int> &post, vector<int> &inorder, int &posti, int begini, int endi)
    {
        if (begini > endi)
            return nullptr;
        TreeNode *root = new TreeNode(post[posti]);
        // 树是肯定存在的，所以不需要判断边界
        int mid = search[post[posti--]];
        root->right = _bulid(post, inorder, posti, mid + 1, endi);
        root->left = _bulid(post, inorder, posti, begini, mid - 1);
        return root;
    }
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
    {
        int posti = postorder.size() - 1;
        for (int i = 0; i < inorder.size(); ++i)
            search.insert({inorder[i], i});
        return _bulid(postorder, inorder, posti, 0, inorder.size() - 1);
    }
};